/* * 
 * File: AchievementView.java
 * @author: Adam Phung
 * Email: adam.phung@gmail.com
 * 
 * Date Created:
 * Date Modified: Oct 31, 2012 
 * Description:
 */

package edu.bu.cs673.superkids123.children.view;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import edu.bu.cs673.superkids123.AbstractFrame;
import edu.bu.cs673.superkids123.AbstractNavigationView;
import edu.bu.cs673.superkids123.children.controller.AchievementController;
import edu.bu.cs673.superkids123.children.model.resources.ChildrenResource;
import edu.bu.cs673.superkids123.children.model.resources.ChildrenResourceProvider;
import edu.bu.cs673.superkids123.children.view.components.AchievementDetails;
import edu.bu.cs673.superkids123.common.model.Achievement;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class AchievementView extends AbstractNavigationView implements
		ActionListener {

	private static final String TEXT_FORMAT = "<html><body><b>%s</b><br/><i>%s</i></body></html>";
	private static final String POINTS_LABEL = "Points: %s \u2605";
	private static final int SCROLL_SPEED = 16;
	private static final int LAYOUT_GAP = 10;

	private JScrollPane listPane = new JScrollPane();
	private JPanel listPanel = new JPanel();
	ChildrenResourceProvider provider = ChildrenResourceProvider.getInstance();

	/** The details panel. */
	private AchievementDetails detailsPanel = new AchievementDetails();

	public AchievementView(AbstractFrame mainFrame) {
		super(mainFrame);
	}

	@Override
	public String getTitle() {
		return "Achievements";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.bu.cs673.superkids123.AbstractNavigationView#getBorderTitle()
	 */
	@Override
	public String getBorderTitle() {
		return "Achievements";
	}

	/**
	 * @wbp.parser.entryPoint
	 */
	@Override
	protected JPanel layout() {

		this.setParentsButtonVisible(false);
		this.setAvatarVisible(false);
		this.setTitleBorderVisible(true);
		
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(1, 2, 5, 5));
		listPanel = new JPanel();
		
		JPanel leftPanel = new JPanel();
		leftPanel.setLayout(new BorderLayout());
		
		// Scroll pane setup
		listPane.setViewportView(listPanel);
		listPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		listPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		listPane.getVerticalScrollBar().setUnitIncrement(SCROLL_SPEED);

		// Panel
		listPanel.setLayout(new GridLayout(0, 1, LAYOUT_GAP, LAYOUT_GAP));
		
		
		// Get Achievements
		ArrayList<Achievement> achievements = getMainFrame().getController(
				AchievementController.class).getAllActiveAchievements();

		// If no achievements are in database add a filler one
		if (achievements.isEmpty()) {
			achievements.add(new Achievement("The Unachievable", "None, sorry...",
					"I couldn't find any achievements so I made you one!"));
		}

		// Get all earned achievements from current logged in user
		List<Achievement> earned = getMainFrame().getController(AchievementController.class).getEarnedAchievements();
		
		// Add earned achievements first
		for (Achievement a : earned) {
			achievements.remove(a);
			JButton button = makeAchievementButton(a);
			button.setEnabled(true);

			listPanel.add(button);			
		}
		
		// Add unearned achievements
		for (Achievement a : achievements) {
			JButton button = makeAchievementButton(a);
			button.setEnabled(false);

			listPanel.add(button);
		}

		// Populate the points label
		int points = getMainFrame().getController(AchievementController.class).getPoints();
		String pointsText = String.format(POINTS_LABEL, points);
		JLabel pointsLabel = new JLabel(pointsText);
		pointsLabel.setPreferredSize(new Dimension(100, 25));
		
		leftPanel.add(pointsLabel, BorderLayout.NORTH);
		leftPanel.add(listPane, BorderLayout.CENTER);
		
		panel.add(leftPanel);
		panel.add(detailsPanel);

		return panel;
	}
	
	private JButton makeAchievementButton(Achievement a) {
		String firstLine = String.format(a.getName());
		String secondLine = String.format(a.getDescription());
		String fullText = String.format(TEXT_FORMAT, firstLine, secondLine);
		
		JButton button = new JButton(fullText);
		button.setHorizontalAlignment(SwingConstants.LEFT);

		button.setIcon(a.getMedalIcon());

		button.addActionListener(this);
		button.setName(a.getName());
		
		return button;
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		if (e.getSource() instanceof JButton) {
			JButton button = (JButton) e.getSource();
			detailsPanel.setSelectedAchievement(getMainFrame().getController(
					AchievementController.class).selectAchievement(
					button.getName()));
		}
	}

}
